package com.pinganfu.mock.test;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolHttpClient {
    // 线程池
    private ExecutorService exe = null;
    // 线程池的容量
    private static  int POOL_SIZE = 5;
    private HttpClient client = null;
    List<String> urlList = null;
    public ThreadPoolHttpClient(List<String> urlList){
        this.urlList=urlList;
    }
    public ConcurrentHashMap<Integer,Boolean> test() throws Exception {
        int uSize = urlList.size();
        if(uSize>0 && uSize<=12){
            POOL_SIZE = 4;
        }
        if(uSize>30){
            POOL_SIZE = 6;
        }
        Date d1 = new Date();
        final CountDownLatch threadSignal = new CountDownLatch(urlList.size());
        final ConcurrentHashMap<Integer,Boolean> hashMap = new ConcurrentHashMap();
        exe = Executors.newFixedThreadPool(POOL_SIZE);
        HttpParams params =new BasicHttpParams();
        /* 从连接池中取连接的超时时间 */
        ConnManagerParams.setTimeout(params, 1000);
        /* 连接超时 */
        HttpConnectionParams.setConnectionTimeout(params, 2000);
        /* 请求超时 */
        HttpConnectionParams.setSoTimeout(params, 4000);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(
                new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

        //ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
        PoolingClientConnectionManager cm=new PoolingClientConnectionManager(schemeRegistry);
        cm.setMaxTotal(10);
        final HttpClient httpClient = new DefaultHttpClient(cm,params);

        // URIs to perform GETs on
        //final String[] urisToGet = urls;
        final List<String> finalUriGet=urlList;

        /*使用线程池*/
        for (int i = 0; i < finalUriGet.size(); i++) {
            final int j=i;
            //System.out.println("j的值:"+j);
            HttpHead httpHead = new HttpHead(finalUriGet.get(j));
            GetThread getThread = new GetThread(httpClient,httpHead,threadSignal,hashMap);
            getThread.setName("线程" + j);
            exe.submit(getThread);
        }
        threadSignal.await();
        exe.shutdown();
        System.out.println("Done");
        System.out.println("毫秒："+(new Date().getTime()-d1.getTime()));

        return hashMap;
    }
    public ConcurrentHashMap<Integer,Boolean> getHashMapByUrls(){
        ThreadPoolHttpClient threadPoolHttpClient = new ThreadPoolHttpClient(urlList);
        try {
            ConcurrentHashMap<Integer,Boolean> hashMap =threadPoolHttpClient.test();
            for(Map.Entry<Integer,Boolean> e:hashMap.entrySet()){
                System.out.println("key:"+e.getKey()+"_____||| value:"+e.getValue());
            }
            return  hashMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
